package com.iplant.aps.serviceimpl.dao.bfc;

import com.iplant.aps.service.mesenum.BFCMessageStatus;
import com.iplant.aps.service.mesenum.BPMEventModule;
import com.iplant.aps.service.po.OutResult;
import com.iplant.aps.service.po.bfc.BFCMessage;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.serviceimpl.CoreServiceImpl;
import com.iplant.aps.serviceimpl.dao.BaseDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

public class BFCMessageDAO extends BaseDAO {

    private static final Logger logger = LoggerFactory.getLogger(BFCMessageDAO.class);

    private static BFCMessageDAO Instance = null;

    private BFCMessageDAO() {
        super();
    }

    public static BFCMessageDAO getInstance() {
        if (Instance == null)
            Instance = new BFCMessageDAO();
        return Instance;
    }

    /**
     * 根据步骤ID查询消息集合
     */
    public List<BFCMessage> GetSetpMessageList(BMSEmployee wLoginUser, int wStepID, List<Integer> wResponsorID,
                                               Calendar wCreateDate, OutResult<Integer> wErrorCode) {
        List<BFCMessage> wResult = new ArrayList<>();
        try {
            wResult = CoreServiceImpl.getInstance().BFC_MsgStep(wLoginUser, wStepID,
                    wResponsorID,
                    wCreateDate).List(BFCMessage.class);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    /**
     * 查询待办任务ID集合
     */
    public List<Integer> BFC_QueryMessageIDToDoList(BMSEmployee wLoginUser, int wBPMEventModule,
                                                    OutResult<Integer> wErrorCode) {
        List<Integer> wResult = new ArrayList<>();
        try {
            //查询近一年的
            Calendar wStartTime = Calendar.getInstance();
            wStartTime.add(Calendar.YEAR, -1);
            Calendar wEndTime = Calendar.getInstance();
            wEndTime.add(Calendar.DATE, 1);

            List<BFCMessage> wList = CoreServiceImpl.getInstance().BFC_GetMessageListByCondition(wLoginUser,
                    wLoginUser.ID, -1, -1,
                    StringUtils.parseListArgs(wBPMEventModule), -1, -1,
                    StringUtils.parseListArgs(BFCMessageStatus.Default.getValue(), BFCMessageStatus.Sent.getValue(),
                            BFCMessageStatus.Read.getValue()), -1, -1, wStartTime
                    , wEndTime).List(BFCMessage.class);
            if (wList != null && wList.size() > 0) {
                wResult = wList.stream().map(p -> (int) p.MessageID).distinct().collect(Collectors.toList());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    /**
     * 查询待办任务ID集合
     */
    public List<Integer> BFC_QueryMessageIDDoneList(BMSEmployee wLoginUser, int wBPMEventModule, Calendar wStartTime,
                                                    Calendar wEndTime,
                                                    OutResult<Integer> wErrorCode) {
        List<Integer> wResult = new ArrayList<>();
        try {
            Calendar wBaseTime = Calendar.getInstance();
            wBaseTime.set(2010, 0, 1, 0, 0, 0);
            if (wStartTime == null || wEndTime == null || wStartTime.compareTo(wBaseTime) <= 0 || wEndTime.compareTo(wBaseTime) <= 0) {
                return wResult;
            }

            List<BFCMessage> wList = CoreServiceImpl.getInstance().BFC_GetMessageListByCondition(wLoginUser,
                    wLoginUser.ID, -1, -1,
                    StringUtils.parseListArgs(wBPMEventModule), -1, -1,
                    StringUtils.parseListArgs(BFCMessageStatus.Finished.getValue()), -1, -1, wStartTime
                    , wEndTime).List(BFCMessage.class);
            if (wList != null && wList.size() > 0) {
                wResult = wList.stream().map(p -> (int) p.MessageID).distinct().collect(Collectors.toList());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    public void CloseMessage(BMSEmployee wLoginUser, int wTaskIPTID, int wTaskType, OutResult<Integer> wErrorCode) {
        try {
            int wModuleID = 0;
            switch (wTaskType) {
                case 6:
                    wModuleID = BPMEventModule.SCZJ.getValue();
                    break;
                case 12:
                    wModuleID = BPMEventModule.MutualCheck.getValue();
                    break;
                case 13:
                    wModuleID = BPMEventModule.SpecialCheck.getValue();
                    break;
                case 14:
                    wModuleID = BPMEventModule.PreCheck.getValue();
                    break;
                default:
                    break;
            }

            CoreServiceImpl.getInstance().BFC_MsgHandle(wLoginUser, wModuleID, null,
                    BFCMessageStatus.Close.getValue(), StringUtils.parseListArgs(wTaskIPTID),
                    -1, 0);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
    }
}
